package org.grayrabbit.cms.publish.generate.factory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.grayrabbit.cms.constant.ConstantTheme;
import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.Category;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.entity.Site;
import org.grayrabbit.cms.entity.Survey;
import org.grayrabbit.cms.entity.Topic;
import org.grayrabbit.cms.publish.api.Generatable;
import org.grayrabbit.cms.publish.api.Publishable;
import org.grayrabbit.cms.vo.content.ContentBean;

import freemarker.template.Configuration;

public class GeneraterFactoryImpl extends AbstractFactory {
	private Configuration configuration;
	private String type;
	private List<String> targets;
	private String rootPath = ConstantTheme.THEME;
	private Site site;
	private int pageSize;
	private int pageNum;
	private List<Category> taxPath;
	private Generatable generatable;
	private Admin admin;
	
	
	public GeneraterFactoryImpl(Configuration config, 
			String genType,
			List<String> targets, 
			String theme,
			Site site, 
			int pageSize,
			int pageNum,
			Generatable generatable,
			Admin admin
			) {
		this.configuration = config;
		this.type = genType;
		this.targets = targets;
		this.pageNum = pageNum;
		this.pageSize = pageSize;
		this.site = site;
		this.generatable = generatable;
		this.admin = admin;
	}

	@Override
	public GeneraterFactory getGeneraterFactory() throws Exception {
		if(Publishable.INDEXT_GEN.equals(type)){
			List<Site> sites = new ArrayList<Site>();
			for (String siteId : targets) {
				sites.add(generatable.getSite(siteId));
			}
			return new IndexGeneraterFactory(configuration, sites,rootPath,generatable,admin);
		}else if(Publishable.CATEGORY_GEN.equals(type)){
			List<Category> categorys = new ArrayList<Category>();
			int i = 0;
			for(String catId : targets){
				categorys.add(generatable.getCategory(catId));
			}
			String[] parentPath = new String[categorys.size()];
			for(Category obj : categorys){
				String path = generatable.getParentPath(obj);
				parentPath[i] = path;
				i++;
			}
			return new CategoryGeneraterFactory(configuration, categorys, rootPath,parentPath,generatable,admin);
		}else if(Publishable.CATEGORY_LIST_GEN.equals(type)){
			List<Category> categorys = new ArrayList<Category>();
			Map<String,List<Content>> contents = new HashMap<String,List<Content>>(); 
			int i = 0;
			for(String catId : targets){
				ContentBean contentBean = new ContentBean();
				contentBean.setCategory(generatable.getCategory(catId));
				List<Content> cList = generatable.findAllByPublish(contentBean,pageSize*pageNum);
				categorys.add(generatable.getCategory(catId));
				contents.put(catId, cList);
			}
			String[] parentPath = new String[categorys.size()];
			for(Category obj : categorys){
				String path = generatable.getParentPath(obj);
				parentPath[i] = path;
				i++;
			}
			
			return new CategoryListGeneraterFactory(configuration, categorys, rootPath, parentPath,contents,pageSize,pageNum,generatable,admin);
		}else if(Publishable.CONTENT_GEN.equals(type)){
			List<Content> contents = new ArrayList<Content>();
			for(String contentId : targets){
					Content content = generatable.getContent(contentId);
					contents.add(content);
			}
			return new ContentGeneraterFactory(configuration,site,contents,rootPath,generatable,admin);
		}else if(Publishable.TOPIC_GEN.equals(type)){
			List<Topic> topics =  new ArrayList<Topic>();
			for(String topicId : targets){
				topics.add(generatable.getTopic(topicId));
			}
			return new TopicGeneraterFactory(configuration, topics, rootPath,generatable,admin); 
		}else if(Publishable.TOPIC_LIST_GEN.equals(type)){
			List<Topic> topics =  new ArrayList<Topic>();
			Map<String,List<Content>> contents = new HashMap<String,List<Content>>(); 
			for(String topicId : targets){
				topics.add(generatable.getTopic(topicId));
				ContentBean contentBean = new ContentBean();
				contentBean.setTopic(generatable.getTopic(topicId));
				contents.put(topicId, generatable.findAllByPublish(contentBean,pageSize*pageNum));
			}
			return new TopicListGeneraterFactory(configuration, topics, rootPath,contents,pageSize,pageNum,generatable,admin);
		}else if(Publishable.SURVEY_GEN.equals(type)){
			List<Survey> surveys =  new ArrayList<Survey>();
			for(String surveyId : targets){
				surveys.add(generatable.getSurvey(surveyId));
			}
			return new SurveyGeneraterFactory(configuration, surveys, rootPath,generatable,admin);
		}else if(Publishable.CATEGORY_GEN_ASY.equals(type)){
			List<Category> categorys = new ArrayList<Category>();
			int i = 0;
			for(String catId : targets){
				categorys.add(generatable.loadCategory(catId));
			}
			String[] parentPath = new String[categorys.size()];
			for(Category obj : categorys){
				parentPath[i] = generatable.getParentPath(obj);
				i++;
			}
			return new CategoryGeneraterFactory(configuration, categorys, rootPath,parentPath,generatable,admin);
		}else{
			throw new Exception("不存在类型为："+type+" 的工厂");
		}
	}
}
